package _1域对象;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


//ServletContext 域对象

//getAttribute(key)
//setAttribute(key,value)
//removeAttribute(key)





@WebServlet("/test2")
public class Test2Servlet extends HttpServlet{
	
	int count3 = 0;
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		int count4 = 0;
		count4++;
		resp.getWriter().println("test2 count4:"+count4+"<br />");
		
		count3++;
		resp.getWriter().println("test2 count3:"+count3+"<br />");
	
		
		//使用ServletContext
		//得到上下文对象
		ServletContext context = this.getServletContext();
		
		//取信息
		Integer allCount = (Integer)context.getAttribute("allCount");
		if(allCount==null) {
			allCount = 0;
		}
		allCount++;
		//存入信息
		context.setAttribute("allCount", allCount);
		
		resp.getWriter().print("allCount:"+allCount+"<br />");
	
	}

}
